#!/bin/csh
# write_timesteps.csh
#  Read output files stored in the simulation directory and writes in the file
#  timesteps.dat the simulation time and the corresponding time step.
#  The script is useful to quickly identify a simulation output when it is
#  necessary to plot it at a known simulation time.
#
# Run it using ./write_timesteps.csh
##################################################################################

source simulation.dir

if (! -d $dirout) then
  echo $dirout 'does not exist. Check the path stored in the variable "dirout" '
  exit
endif

set outfile = timesteps.dat
touch $outfile

#------------------------------------------------------
# Make a loop over all output files found in dirout
#------------------------------------------------------
set files = (`ls ${dirout}profiles*.dat`)
   foreach file ($files)
   echo "Current file:" $file

# Extract the current time-step of the simulation
   set tstep = `awk 'NR == 1 {print $3}' $file`

# Extract the current time of the simulation from the corresponding grd file
   set ctime = `awk 'NR == 1 {print $5}' $file`

# Write timestep and simulation time 
   awk 'BEGIN {printf "%s %s \n", "'"$ctime"'", "'"$tstep"'"}' >> $outfile
   
end

# Move output file into the parent simulation directory
mv $outfile $dirout 
